﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace MyPublisher.Composition.Fop
{
    [Transform(
        Name = "DITA.Dita2Pdf",
        Provider = "DITA", 
        InputType = FragmentType.DITA, 
        OutputType = FragmentType.PDF)]
    public class Dita2PdfTranform : ITransform
    {
        public const string DITA_DIR = @"D:\mypublisher\lib\DITA-OT1.5";
        public const string DITA_COMMAND = "dita";

        public void Tranform(Fragments inputs, Fragment output)
        {
            //dita /i:C:/temp/currywurst.xml /transtype:pdf /outdir:z:/
            //Mandatory parameters:
            //  /i:                    specify path and name of the input file
            //  /transtype:            specify the transformation type
            //Options:
            //  -help, -h              print this message
            //  -version               print the version information and exit
            //  /basedir:              specify the working directory
            //  /ditadir:              specify the toolkit's home directory. Default is "temp"
            //  /outdir:               specify the output directory
            //  /tempdir:              specify the temporary directory
            //  /logdir:               specify the log directory
            //  /ditaext:              specify the file extension name to be used in the temp directory. Default
            //  /filter:               specify the name of the file that contains the filter/flaggin/revision in
            //  /draft:                specify whether to output draft info. Valid values are "no" and "yes". De
            //  /artlbl:               specify whether to output artwork filenames. Valid values are "no" and "y
            //  /ftr:                  specify the file to be placed in the BODY running-footing area
            //  /hdr:                  specify the file to be placed in the BODY running-heading area
            //  /hdf:                  specify the file to be placed in the HEAD area
            //  /csspath:              specify the path for css reference
            //  /css:                  specify user css file
            //  /cssroot:              specify the root directory for user specified css file
            //  /copycss:              specify whether to copy user specified css files. Valid values are "no" a
            //  /indexshow:            specify whether each index entry should display within the body of the te
            //  /outext:               specify the output file extension for generated xhtml files. Default is "
            //  /xsl:                  specify the xsl file used to replace the default xsl file
            //  /cleantemp:            specify whether to clean the temp directory before each build. Valid valu
            //  /foimgext:             specify the extension of image file in pdf transformation. Default is ".j
            //  /javahelptoc:          specify the root file name of the output javahelp toc file in javahelp tr
            //  /javahelpmap:          specify the root file name of the output javahelp map file in javahelp tr
            //  /eclipsehelptoc:       specify the root file name of the output eclipsehelp toc file in eclipseh
            //  /eclipsecontenttoc:    specify the root file name of the output Eclipse content provider toc fil
            //  /xhtmltoc:             specify the root file name of the output xhtml toc file in xhtml transfor
            //  /validate:             specify whether the ditamap/dita/xml files to be validated
            //  /outercontrol:         specify how to respond to the overflowing dita/topic files. Only "fail",
            //  /generateouter:        specify how to deal with the overflowing dita/topic files. Only "1", "2"
            //the output directory. Option 3: the old solution,adjust the input.dir according to the referenced
            //  /onlytopicinmap:       specify whether the referenced dita/topic files which are not referenced
            Fragments outputs = new Fragments() { output };
            string outdir = output.Path;
            string path = output.Path;

            string inputpath = inputs[0].Path;
            string tmppath = inputpath.Substring(0, inputpath.LastIndexOf("\\"))+"\\tmp";
            string parameter = string.Format(@"/i:{0} /tempdir:{1} /transtype:pdf /ditadir:{2} /outdir:{3}",
                inputpath, 
                tmppath,
                DITA_DIR,
                outdir);

            try
            {
                ProcessStartInfo psi = new ProcessStartInfo(DITA_COMMAND, parameter);
                psi.UseShellExecute = true;
                psi.WindowStyle = ProcessWindowStyle.Hidden;
                Process.Start(psi);
            }
            catch (Exception e)
            {
                throw new CompositionException("FopComposition failed to start FOP process.", e);
            }
        }
    }
}
